<?php

/**
 * bolsa actions.
 *
 * @package    tvpv
 * @subpackage bolsa
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 9301 2008-05-27 01:08:46Z dwhittle $
 */
class bolsaActions extends sfActions
{
	/**
	 * Executes index action
	 *
	 * @param sfRequest $request A request object
	 */
	public function executeIndex($request)
	{
		$directorioPrototype = sfConfig::get('sf_prototype_web_dir');
		$this->getResponse()->addJavascript($directorioPrototype.'/js/prototype');
	}

	public function executeActualizar($request){
		$art = ArticuloPeer::retrieveByPK($this->getRequestParameter('art'));
		if( $request->getParameter('cantidad')<= $art->getExistencia())
		{
			$articuloList = $this->getUser()->getAttribute('articulos');
			$total = $this->getUser()->getAttribute('total');
			$diferencia = $request->getParameter('cantidad');
			$diferencia -= $articuloList[$request->getParameter('art')]['cantidad'];
			$articuloList[$request->getParameter('art')]['cantidad'] = $request->getParameter('cantidad');
			$total += ( $articuloList[$request->getParameter('art')]['precio'] * $diferencia) ;
			$this->getUser()->setAttribute('cantidad',$this->getUser()->getAttribute('cantidad') + $diferencia);
			$this->getUser()->setAttribute('cantidad',$request->getParameter('cantidad'));
			$this->getUser()->setAttribute('articulos',$articuloList);
			$this->getUser()->setAttribute('total',$total);
			$this->getUser()->setFlash('msj','Se actualizó la lista de articulos');
		}
		else
		  $this->getUser()->setFlash('msj','La cantidad de artículos es mayor a la existencia');
		$this->redirect('bolsa/index');
	}

	public function executeDelete($request){
		$articuloList = $this->getUser()->getAttribute('articulos');
		$total = $this->getUser()->getAttribute('total');
		$cantidad = $this->getUser()->getAttribute('cantidad');
		$cantidad -= $articuloList[$request->getParameter('id')]['cantidad'];
		$total -= $articuloList[$request->getParameter('id')]['cantidad'] * $articuloList[$request->getParameter('id')]['precio'];
		unset($articuloList[$request->getParameter('id')]);
		$this->getUser()->setAttribute('articulos',$articuloList);
		$this->getUser()->setAttribute('total',$total);
		$this->getUser()->setAttribute('cantidad',$cantidad);
		$this->redirect('bolsa/index');
	}

	public function executePagado($request){
		$this->redirectIf($request->isMethod('get'),"main/index");
		$this->venta = new VentaForm();
		$x = 0;
		for($i=1; $i<= $request->getParameter("num_cart_items"); $i++){
			$x += $request->getParameter("mc_gross_$i");
		}
		$venta_array = array( "transaccionid"=>$request->getParameter("txn_id"),
  	                      "compradorid"=>$request->getParameter("payer_id"),
  	                      "compradoremail"=>$request->getParameter("payer_email"),
  	                      "compradorstatus"=>$request->getParameter("payer_status"),
  	                      "memo"=>$request->getParameter("memo"),
  	                      "pagostatus"=>$request->getParameter("payment_status"),
  	                      "subtotal"=>$x,
  	                      "total"=>$request->getParameter("mc_gross"),  	
		);
		//$this->forward404If()
		$this->venta->bind($venta_array);
		if($this->venta->isValid()){
			$this->getUser()->setFlash("Valido","Formulario Valido");
			$this->venta->save();
			for($i=1; $i<= $request->getParameter("num_cart_items"); $i++){
				$articulo = new ArticuloVentaForm();
				//$this->logMessage("------------>".$request->getParameter("item_number$i"));
				$articulo_array = array('venta_id'=> $this->venta->getObject()->getId(),
  			                  'articulo_id'=> ArticuloPeer::getIdByCodigo($request->getParameter("item_number$i")),
  			                  'cantidad'=>$request->getParameter("quantity$i"),
  			                  'detalle1'=>$request->getParameter("option_name1_$i").$request->getParameter("option_selection1_$i"),
  			                  'detalle2'=>$request->getParameter("option_name2_$i").$request->getParameter("option_selection2_$i"));
				$articulo->bind($articulo_array);
				if($articulo->isValid())
				{
					$tmp = ArticuloPeer::retrieveByPK(ArticuloPeer::getIdByCodigo($request->getParameter("item_number$i")));
					$tmp->setExistencia($tmp->getExistencia() - $request->getParameter("quantity$i") );
					$tmp->save();
				  $articulo->save();
				}
				else
				$this->getUser()->setFlash("Valido","Articulo Invalido");
			}
			$this->getUser()->getAttributeHolder()->clear();

		}
		else
		{
			if($this->venta->getFormFieldSchema()->getError())
			$this->redirect("main/index");
			//$field->next()
			//$this->getUser()->setFlash("Valido","Formulario INValido--.".$field->getError());

		}
	}


}
